//
//  UnloadingAndHangBoxVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/1/13.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "UnloadingAndHangBoxVC.h"
#import "UnloadingAndHangBoxCell.h"
#import "UnloadingAndHangBoxShowListVC.h"
#import "UnloadingAndHangBoxChooseVC.h"
#import "UIButton+Expand.h"

@interface UnloadingAndHangBoxVC ()
// 选择集装箱数组
@property (strong, nonatomic) NSMutableArray *containerDataArray;
// 全选标示
@property (copy, nonatomic) NSString *selectAllMark;

@end

@implementation UnloadingAndHangBoxVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
    // 设置BarButtonItem
    [self setBarButtonItem];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = [self.typeJudge isEqualToString: @"Unloading"] ? @"卸箱" : @"吊箱";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[UnloadingAndHangBoxCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[UnloadingAndHangBoxCell reuseIdentifier]];
}

// 设置BarButtonItem
- (void)setBarButtonItem
{
    UIButton *itemBtn = [UIButton setLeftOrRightButton:nil direction:@"right" title:@"确定"];
    [itemBtn addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:itemBtn];
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)containerDataArray
{
    if (!_containerDataArray) {
        _containerDataArray = [[NSMutableArray alloc]init];
    }
    return _containerDataArray;
}

#pragma mark - Action
// 查询 Action
- (IBAction)queryAction:(UIButton *)sender
{
    // 跳转到送修选择页面
    UnloadingAndHangBoxChooseVC *unloadingAndHangBoxChooseVC = [[UnloadingAndHangBoxChooseVC alloc]init];
    // 测试使用的数据判断
    if ([self.queryConditions.text isEqualToString:@"1"]) {
        unloadingAndHangBoxChooseVC.type = @"1";
    }else{
        unloadingAndHangBoxChooseVC.type = @"2";
    }
    unloadingAndHangBoxChooseVC.containerDataArray = [NSMutableArray arrayWithArray:self.containerDataArray];
    // 防止循环引用
    WEAK_REF(self);
    // 复选框点击 Block
    [unloadingAndHangBoxChooseVC setCheckImageClickBlock:^(NSString *content, BOOL state){
        //======修改Model 自定义字段======
        if (state) {
            [weak_self.containerDataArray addObject:content];
        }else{
            // 移除指定元素
            [weak_self.containerDataArray removeObject:content];
        }
        // 刷新Cell
        [weak_self.myTableView reloadData];
    }];
    // 全选复选框点击 Block
    [unloadingAndHangBoxChooseVC setAllCheckImageClickBlock:^(NSMutableArray *dataArray, BOOL state){
        // 遍历数据数组
        for (int i = 0 ; i < dataArray.count; i++) {
            // 查询数组中是否保存集装箱编号
            BOOL isbool = [weak_self.containerDataArray containsObject:dataArray[i]];
            if (state) {
                // 全选
                if (!isbool) {
                    [weak_self.containerDataArray addObject:dataArray[i]];
                }
            }else{
                // 取消全选
                if (isbool) {
                    [weak_self.containerDataArray removeObject:dataArray[i]];
                }
            }
        }
        // 刷新Cell
        [weak_self.myTableView reloadData];
    }];
    [self.navigationController pushViewController:unloadingAndHangBoxChooseVC animated:YES];
}

// 确定 Action
- (void)rightItemAction
{
    UnloadingAndHangBoxShowListVC *unloadingAndHangBoxShowListVC = [[UnloadingAndHangBoxShowListVC alloc]init];
    unloadingAndHangBoxShowListVC.containerDataArray = _containerDataArray;
    [self.navigationController pushViewController:unloadingAndHangBoxShowListVC animated:YES];
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.containerDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UnloadingAndHangBoxCell *cell = [tableView dequeueReusableCellWithIdentifier:[UnloadingAndHangBoxCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    [cell setData:[NSString stringWithFormat:@"%@",self.containerDataArray[indexPath.row]]];
    return cell;
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 移除数据
        [_containerDataArray removeObjectAtIndex:indexPath.row];
        // 移除单元格
        //        [_myTableView reloadData];
        [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// 设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
